package com.z.common.configure.security;

import com.z.common.aop.enums.BusinessType;
import com.z.common.aop.enums.LoggingType;
import com.z.common.aop.enums.RequestModel;
import com.z.common.constant.ServletConstant;
import com.z.common.context.UserContext;
import com.z.common.utils.SequenceUtil;
import com.z.sys.entity.SysLog;
import com.z.sys.mapper.SysLogMapper;
import com.z.common.web.vo.response.Result;
import com.z.sys.entity.User;
import com.z.sys.mapper.UserMapper;
import com.z.sys.vo.UserInfoVo;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@Component
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Resource
    private SysLogMapper sysLogMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.setStatus(HttpStatus.OK.value());
        SysLog sysLog = new SysLog();
        sysLog.setId(SequenceUtil.makeStringId());
        sysLog.setTitle("登录");
        sysLog.setDescription("登录成功");
        sysLog.setBusinessType(BusinessType.OTHER);
        sysLog.setSuccess("1");
        sysLog.setLoggingType(LoggingType.LOGIN);
        sysLog.setOperateAddress(httpServletRequest.getRemoteHost());
        sysLog.setMethod(httpServletRequest.getRequestURI());
        sysLog.setCreateTime(new Date());
        sysLog.setRequestMethod(RequestModel.valueOf(httpServletRequest.getMethod()));
        sysLog.setOperateUrl(httpServletRequest.getRequestURI());
        String userAgent = httpServletRequest.getHeader(ServletConstant.Header.UA);
        String browser;
        if (userAgent.contains(ServletConstant.Browser.FIRE_FOX_UA)) {
            browser = ServletConstant.Browser.FIRE_FOX_NAME;
        } else if (userAgent.contains(ServletConstant.Browser.CHROME_UA)) {
            browser = ServletConstant.Browser.CHROME_NAME;
        } else if (userAgent.contains(ServletConstant.Browser.IE_UA)) {
            browser = ServletConstant.Browser.IE_NAME;
        } else {
            browser = ServletConstant.Browser.UNKNOWN;
        }
        String systemOS;
        if (userAgent.toLowerCase().contains(ServletConstant.System.WIN_UA)) {
            systemOS = ServletConstant.System.WIN_NAME;
        } else if (userAgent.toLowerCase().contains(ServletConstant.System.MAC_UA)) {
            systemOS = ServletConstant.System.MAC_NAME;
        } else if (userAgent.toLowerCase().contains(ServletConstant.System.UNIX_UA)) {
            systemOS = ServletConstant.System.UNIX_NAME;
        } else if (userAgent.toLowerCase().contains(ServletConstant.System.ANDROID_UA)) {
            systemOS = ServletConstant.System.ANDROID_NAME;
        } else if (userAgent.toLowerCase().contains(ServletConstant.System.IPHONE_UA)) {
            systemOS = ServletConstant.System.IPHONE_NAME;
        } else {
            systemOS = ServletConstant.System.UNKNOWN + userAgent;
        }
        sysLog.setBrowser(browser);
        sysLog.setRequestBody(httpServletRequest.getQueryString());
        sysLog.setSystemOs(systemOS);

        User user = (User) authentication.getPrincipal();
        user.setPassword("");
        httpServletRequest.getSession().setAttribute("currentUser", authentication.getPrincipal());

        User currentUser = UserContext.currentUser();
        sysLog.setOperateName(null != currentUser ? currentUser.getUsername() : "未登录用户");
        sysLogMapper.insert(sysLog);
        UserInfoVo userInfoVo = null;
        try {
            userInfoVo = userMapper.selectUserInfoByUserId(user.getId());
        } catch (Exception e){
            e.printStackTrace();
            httpServletResponse.getWriter().write(Result.error("缺少配置").toString());
        }
        httpServletResponse.getWriter().write(Result.success( userInfoVo ).toString());
    }
}